package com.woniu.service.healthSystem.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.mapper.healthSystem.RolePermissionMapper;
import com.woniu.model.healthSystem.dto.RolePermissionDto;
import com.woniu.model.healthSystem.entity.RolePermission;
import com.woniu.service.healthSystem.IRolePermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 角色对应权限表 服务实现类
 * </p>
 *
 * @author gb
 * @since 2023-10-19
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {

    @Transactional
    @Override
    public void roleAuthorization(RolePermissionDto rolePermissionDto) {
        //先删除原有的角色对应的权限：delete from role_user_permission where role_id=1
        baseMapper.delete(Wrappers.lambdaQuery(RolePermission.class).eq(RolePermission::getRoleId, rolePermissionDto.getRoleId()));
        // 遍历所有的权限，添加角色权限关系： insert into role_user_permission(url_permission_id,role_id)..
        Long[] permissionIds = rolePermissionDto.getPermissionIds();
        if (permissionIds != null && permissionIds.length > 0) {
            for (Long permissionId : permissionIds) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(rolePermissionDto.getRoleId());
                rolePermission.setPermissionId(permissionId);
                baseMapper.insert(rolePermission);
            }
        }
    }
}
